﻿Public Class FormPedidosNListado

    Dim classController As New Class_RC_Controller.rc_Notas()
    Dim clientes As New Class_RC_Controller.rc_Clientes

    Private m_frmPedido As FrmPedidosN
    Public Property frmPedido() As FrmPedidosN
        Get
            Return m_frmPedido
        End Get
        Set(value As FrmPedidosN)
            m_frmPedido = value
        End Set
    End Property

    Private Sub FormPedidosListado_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        dgPedidos.AutoGenerateColumns = False
        fechaInicio.Value = DateTime.Now.AddDays(-5)

        CargarCombos()
        CargarNotas()
    End Sub

    Private Sub CargarNotas()

        Dim codEstado As Integer = 0
        If cbxEstado.Checked Then
            codEstado = cboEstado.SelectedValue
        End If

        Dim dt As New DataTable
        dt = classController.get_Notas_All(oiml.dbConn, 0, fechaInicio.Value, fechaFin.Value, 0, txtNombre.Text,
        codEstado, txtNro.Text, txtRuc.Text, txtContacto.Text, False, 0)
        dgPedidos.DataSource = dt
        dgPedidos.Select()

        If Not classController.MessageError Is Nothing Then
            MessageBox.Show("Error: " & classController.MessageError.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If

    End Sub

    Private Function CargarNotasReporte() As DataTable

        Dim codEstado As Integer = 0
        If cbxEstado.Checked Then
            codEstado = cboEstado.SelectedValue
        End If

        Dim dt As New DataTable
        dt = classController.get_Notas_Impresion(oiml.dbConn, 0, fechaInicio.Value, fechaFin.Value, 0, txtNombre.Text,
        codEstado, txtNro.Text, txtRuc.Text, txtContacto.Text)

        If Not dt Is Nothing Then
            Return dt
        Else
            MessageBox.Show("Error: " & classController.MessageError.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return Nothing
        End If

    End Function

    Private Sub CargarCombos()
        Dim ds As DataTable
        ds = classController.get_estado_notas(oiml.dbConn, 0)
        With cboEstado
            .DataSource = ds
            .DisplayMember = "descEstadoNota"
            .ValueMember = "codEstadoNota"
        End With

        cboEstado.SelectedIndex = 0

    End Sub

    Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()

    End Sub

    Private Sub btnEditar_Click(sender As System.Object, e As System.EventArgs) Handles btnEditar.Click
        Try
            If dgPedidos.SelectedRows.Count > 0 Then
                Dim cod As New Integer
                cod = dgPedidos.SelectedRows(0).Cells(0).Value

                If frmPedido Is Nothing Then

                    frmPedido = New FrmPedidosN()
                    frmPedido.codNota = cod

                    'If frmClientes.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    frmPedido.ShowDialog()
                    CargarNotas()
                    frmPedido.Dispose()
                    frmPedido = Nothing
                    'End If
                Else
                    frmPedido.codNota = cod
                    frmPedido.ShowDialog()
                End If

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub txtBuscar_Click(sender As System.Object, e As System.EventArgs) Handles txtBuscar.Click
        CargarNotas()
    End Sub

    Private Sub txtNombre_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtNombre.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarNotas()

        End If
    End Sub

    Private Sub dgPedidos_DoubleClick(sender As System.Object, e As System.EventArgs) Handles dgPedidos.DoubleClick
        btnEditar_Click(sender, e)
    End Sub

    Private Sub dgPedidos_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles dgPedidos.KeyDown
        If e.KeyCode = Keys.Enter Then
            btnEditar_Click(sender, e)
        End If

    End Sub



    Private Sub txtRuc_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtRuc.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarNotas()

        End If
    End Sub

    Private Sub txtNro_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtNro.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarNotas()

        End If
    End Sub

    Private Sub cbxEstado_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles cbxEstado.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarNotas()

        End If
    End Sub

    Private Sub cboEstado_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles cboEstado.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarNotas()

        End If
    End Sub

    Private m_frmRpts As FormReportes
    Public Property frmRpts() As FormReportes
        Get
            Return m_frmRpts
        End Get
        Set(value As FormReportes)
            m_frmRpts = value
        End Set
    End Property

 
    
    Private Sub txtContacto_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtContacto.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarNotas()
        End If

    End Sub


    Private Sub btnImprimir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImprimir.Click

        txtBuscar_Click(sender, e)

        If frmRpts Is Nothing Then
            frmRpts = New FormReportes()
            'frmRpts.MdiParent = Me
            frmRpts.tipo_Reporte = "PEDIDOS"
            frmRpts.datos_Reporte = CargarNotasReporte()
            frmRpts.ShowDialog()
        Else

            If Not frmRpts.Visible Then
                frmRpts = New FormReportes()
                'frmRpts.MdiParent = Me
                frmRpts.tipo_Reporte = "PEDIDOS"
                frmRpts.datos_Reporte = CargarNotasReporte()
                frmRpts.ShowDialog()
            Else
                frmRpts.tipo_Reporte = "PEDIDOS"
                frmRpts.datos_Reporte = CargarNotasReporte()
                frmRpts.Select()
                frmRpts.Activate()
            End If

        End If
    End Sub
End Class